package com.party.admin.web.controller.wechat;

import com.google.common.base.Strings;
import com.party.admin.biz.system.MemberMerchantBizService;
import com.party.admin.web.dto.AjaxResult;
import com.party.common.paging.Page;
import com.party.core.model.member.DeployType;
import com.party.core.model.wechat.WechatMerchants;
import com.party.core.model.wechatPlatform.WechatOpenMember;
import com.party.core.service.wechat.IWechatMerchantsService;
import com.party.core.service.wechatPlatform.IWechatOpenMemberService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 微信公众号控制器
 * Created by wei.li.
 *
 * @date 2018/1/23 0023
 * @time 17:07
 */
@Controller
@RequestMapping(value = "wechat/account")
public class WechatAccountController {

    @Autowired
    private IWechatOpenMemberService wechatOpenMemberService;

    @Autowired
    private MemberMerchantBizService memberMerchantBizService;

    @Autowired
    private IWechatMerchantsService wechatMerchantsService;

    Logger logger = LoggerFactory.getLogger(getClass());
    /**
     * 微信公众号列表
     *
     * @param wechatOpenMember 公众号信息
     * @param page 分页参数
     * @return 交互数据
     */
    @RequestMapping(value = "list")
    public ModelAndView list(WechatOpenMember wechatOpenMember, Page page){
        page.setLimit(10);
        ModelAndView modelAndView = new ModelAndView("/wechat/wechatAccountList");
        Map<Integer, String> types = DeployType.convertMap();
        List<WechatOpenMember> list = wechatOpenMemberService.listPage(wechatOpenMember, page);
        modelAndView.addObject("page", page);
        modelAndView.addObject("list", list);
        modelAndView.addObject("types", types);
        return modelAndView;
    }


    /**
     * 微信公众号视图
     *
     * @param id 编号
     * @return 交互数据
     */
    @RequestMapping(value = "view")
    public ModelAndView view(String id){
        ModelAndView modelAndView = new ModelAndView("/wechat/wechatAccountView");
        WechatOpenMember wechatOpenMember;
        if (Strings.isNullOrEmpty(id)){
            wechatOpenMember = new WechatOpenMember();
        }
        else {
            wechatOpenMember = wechatOpenMemberService.get(id);
        }
        modelAndView.addObject("wechatOpenMember", wechatOpenMember);
        return modelAndView;
    }

    /**
     * 保存数据
     *
     * @param wechatOpenMember 公众号信息
     * @return 交互数据
     */
    @ResponseBody
    @RequestMapping(value = "save")
    public AjaxResult save(WechatOpenMember wechatOpenMember){
        AjaxResult ajaxResult = new AjaxResult(true);
        if (Strings.isNullOrEmpty(wechatOpenMember.getId())){
            Integer deployType = null == wechatOpenMember.getDeployType()
                    ? DeployType.FILL_IN.getCode() : wechatOpenMember.getDeployType();
            wechatOpenMember.setDeployType(deployType);
        }
        try {
            ajaxResult = memberMerchantBizService.save(wechatOpenMember);
        } catch (Exception e) {
            ajaxResult.setSuccess(false);
            ajaxResult.setDescription(e.getMessage());
            logger.error("保存微信账号信息异常", e);
            return ajaxResult;
        }

        return ajaxResult;
    }


    /**
     * 根据类型填写
     *
     * @param deployType 类型
     * @return 交互数据
     */
    @ResponseBody
    @RequestMapping(value = "findByDeployType")
    public AjaxResult findByDeployType(Integer deployType, String memberId){
        AjaxResult ajaxResult = new AjaxResult(true);
        WechatOpenMember wechatOpenMember = new WechatOpenMember();
        wechatOpenMember.setDeployType(deployType);
        List<WechatOpenMember> wechatOpenMemberList = wechatOpenMemberService.listNoRelation(deployType, memberId);
        List<WechatMerchants> wechatMerchantsList = wechatMerchantsService.listNoRelation(deployType, memberId);
        Map<String, Object> map = new HashMap<>();
        map.put("wechatOpenMemberList", wechatOpenMemberList);
        map.put("wechatMerchantsList", wechatMerchantsList);
        ajaxResult.setData(map);
        return ajaxResult;
    }
}
